<?php
namespace app\admin\controller;
use think\Request;
use think\Session;
use app\common\controller\Backend;
use app\common\model\BackendUserModel;
use app\common\model\BackendUserGroupModel;

class BackendUser extends Backend
{
  //用户列表页
  public function index(Request $request = null){
    $BackendUserModel = new BackendUserModel();
    $userList = $BackendUserModel->getUserList($request->get());
    //获取全部权限组
    $BackendUserGroupModel = new BackendUserGroupModel();
    $userGroup = $BackendUserGroupModel->getAllUserGroup();
    $userGroup = array_column($userGroup, "group_name","id");
    
    return view('index',['userList'=>$userList,'userGroup'=>$userGroup]);
  }

  //创建页面
  public function create(Request $request = null){
    if(Request::instance()->isPost()){
      $post = $request->post();
      $BackendUserModel = new BackendUserModel();
      if($BackendUserModel->where(['username'=>$post['username']])->count()){
        $this->error("该账户名字已经存在！");
      }

      $result = $BackendUserModel->allowField(true)->validate(true)->save($post);
      
      if(false === $result){
        // 验证失败 输出错误信息
        $this->error($BackendUserModel->getError());
      }else{
        $this->success("成功！");
      }
    }else{
      //获取权限组列表
      $BackendUserGroupModel = new BackendUserGroupModel();
      $userGroup = $BackendUserGroupModel->getAllUserGroup();

      return view("create",['userGroup'=>$userGroup]);
    }
  }

  //创建页面
  public function update(Request $request = null){
    if(Request::instance()->isPost()){
      $post = $request->post();
      $BackendUserModel = new BackendUserModel();
      $userModel = $BackendUserModel->getUserById($post['id']);
      $post['salt'] = $userModel['salt'];
      if($BackendUserModel->where(['username'=>$post['username'],'id'=>['neq',$post['id']]])->count()){
        $this->error("该账户名字已经存在！");
      }

      if($post['password'] == ''){
        unset($post['password']);
      }else{
        $post['password'] = $BackendUserModel->encryptionPassword($post['password'], $userModel['salt']);
      }

      $result = $BackendUserModel->allowField(true)->validate(true)->save($post, ['id'=>$post['id']]);
      
      if(false === $result){
        // 验证失败 输出错误信息
        $this->error($BackendUserModel->getError());
      }else{
        $this->success("成功！");
      }
    }else{
      $id = $request->param("id");
      //获取权限组列表
      $BackendUserGroupModel = new BackendUserGroupModel();
      $userGroup = $BackendUserGroupModel->getAllUserGroup();

      $BackendUserModel = new BackendUserModel();
      $userModel = $BackendUserModel->getUserById($id);

      return view("update",['userGroup'=>$userGroup,'userModel'=>$userModel]);
    }
  }

  //后台个人中心页面
  public function user(Request $request = null){
    if(Request::instance()->isPost()){
      $post = $request->post();

      $BackendUserModel = new BackendUserModel();
      $BackendUser = Session::get("backendUser");
      $result = $BackendUserModel->resetPassword($BackendUser['id'],$post['old-password'],$post['password'],$post['repassword']);
      // dump( $result );die();
      if(false === $result){
        // 验证失败 输出错误信息
        $this->error($BackendUserModel->geterror());
      }else{
        $this->success("修改密码成功！");
      }
      
    }else{
      return view("user");
    }
    
  }

	//登录页面
  public function login()
  {
    return view('login');
  }

  //登录动作
  public function dologin(Request $request = null)
  {
    $post = $request->post();
    $BackendUserModel = new BackendUserModel();
    if(!captcha_check($request->post('checkcode'))){
      return json(['status'=>400,'msg'=> '验证码错误！']);
    };

    if($BackendUserModel->login($post['username'],$post['password'])!==false){
      return json(['status'=>200,'msg'=>'登录成功']);
    }else{
      return json(['status'=>400,'msg'=>$BackendUserModel->geterror()]);
    }
  }

  //清除缓存
  public function logout(){
    Session::clear();
    $this->redirect("/");
  }

  //改变用户状态
  public function changeStatus(){
    $status = Request::instance()->post("status");
    $ids = $_POST['id'];
    if(is_array($ids)){return json(['status'=>400,'msg'=>'id不符合格式！']);}
    $ids = [$ids];
    $BackendUserModel = new BackendUserModel();
    if($BackendUserModel->changeStatus($ids, $status) > 0){
      return json(['status'=>200,'msg'=>'成功']);
    }else{
      return json(['status'=>400,'msg'=>'失败']);
    }
  }
  
}
